//获取应用实例
var app = getApp()
Page({
data: {
    userInfo: {},
    wishUser:{},
    voucherName:"",
    voucherMoney:0,
    voucherOtherName:"",
    voucherOtherMoney:0,
    finalMoney:299,
    totalMoney:299,
    voucherIds:"",
    voucherMoneyAll:0,
    cardType:""
  },
onLoad: function (options) {
    console.log(options);
     var that = this
     that.setData({
        cardType:options.cardType
      })
    //调用应用实例的方法获取全局数据
    app.getUserInfo(function(userInfo){
      //更新数据
      that.setData({
        userInfo:userInfo
      })
      wx.request({
        url: app.globalData.path+'/weixin/coffee/xcx/user/getUserInfo',
        data: {
          userId: userInfo.userId
        },
        method: 'GET',
        success: function (res) {
            console.log(res.data)
          that.setData({
            wishUser:res.data
          })
        }
      }) 
      if (that.data.cardType==4){
        that.setData({
          totalMoney: 29,
          finalMoney:29
        })
      }
    })
  },
  submit:function(){
    var that=this
    wx.request({
     url: app.globalData.path+'/wish/createOrder',
     data: {
          userId:that.data.wishUser.userId,
          memberId : that.data.wishUser.code,
					nickName : that.data.wishUser.nikeName,
					voucherId : that.data.voucherIds,
					voucherMoney : that.data.voucherMoneyAll,
          cardType:that.data.cardType
     },
     method: 'GET',
     success: function(res){
          var id = res.data.orderId;
					that.clickPayFor(id);
     }
   })
  },
  clickPayFor:function(orderId){
    
    var that=this
    console.info(that.data.userInfo.openId);
      wx.request({
        url: app.globalData.path +'/weixin/coffee/xcx/pay/index',
        data: {
          orderId:orderId,
          openId:that.data.userInfo.openId
        },
        method: 'GET',
        success: function(res){
          console.info(res.data)
           that.pay(res.data);
        }
      })
  },
  pay:function(res){
    wx.requestPayment({
      'timeStamp': res.orderInfo.split(",")[0],
      'nonceStr': res.orderInfo.split(",")[1],
      'package': res.orderInfo.split(",")[2],
      'signType':'MD5',
      'paySign':res.orderInfo.split(",")[3],
      'success':function(res){
        console.info("支付成功")
            wx.redirectTo({
              url: '../index/index'
            })
          }
     })
  },
  //去优惠券页面
  toMyVoucherList:function(e){
    var packageId = e.currentTarget.dataset.packageid;
    wx.navigateTo({
      url: '../voucherList/voucherList?packageId='+packageId
    })
  }

})